﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAPCVM.Interfaces;
using System.IO;
using System.Drawing;
namespace AAPCVM
{
    public class Controller : IController 
    {
        public event Action<string> OnNOTA;
        private List<string> candidateIds = new List<string>();

        #region IController Members

        public System.Drawing.Image GetCandidateImage(string candidateId)
        {
            string picDirectory = AppDomain.CurrentDomain.BaseDirectory + "Candidates";
            string filepath = picDirectory + "\\" + candidateId + ".jpg";

            Image candidateImage = null;
            try
            {
                if (File.Exists(filepath))
                {
                    candidateImage = Image.FromFile(filepath);
                }
            }
            catch (Exception Ex)
            {
                candidateImage = null;
            }

            return candidateImage;
        }

        public bool IsIdDuplicate(string candidateId)
        {
            return candidateIds.Contains(candidateId);
        }

        public void AddCandidate(string candidateId)
        {
            if (IsNOTA(candidateId))
            {
                if (OnNOTA != null)
                    OnNOTA(candidateId);

                return;
            }

            if (candidateIds.Count == 0)
            {
                candidateIds.Add("00");
            }

            candidateIds.Add(candidateId);
        }

        public bool IsNOTA(string candidateId)
        {
            return candidateId == "00";
        }

        public void Reset()
        {
            candidateIds.Clear();
        }

        #endregion
    }
}
